package com.slipper.util;

import com.slipper.PaasContext;
import com.slipper.constants.Constants;
import com.slipper.core.file.FileServiceUtil;
import com.slipper.core.file.vo.FileVO;
import com.slipper.exception.CommonException;
import com.slipper.system.affix.bo.AffixItemBO;
import com.slipper.system.affix.service.AffixItemService;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Base64;

@Component
public class AffixUtil {
    public static String saveFile(String trueName, String base64) throws Exception {
        if (Tool.isBlank(base64)) throw new CommonException("附件不能为空！");
        return saveFile(null, trueName, Base64.getDecoder().decode(base64));
    }

    public static String saveFile(String affixId, String trueName, String base64) throws Exception {
        if (Tool.isBlank(base64)) throw new CommonException("附件不能为空！");
        return saveFile(affixId, trueName, Base64.getDecoder().decode(base64));
    }

    public static String saveFile(String trueName, InputStream inputStream) throws Exception {
        if (inputStream == null) throw new CommonException("附件不能为空！");
        return saveFile(null, trueName, inputStream.readAllBytes());
    }

    public static String saveFile(String affixId, String trueName, InputStream inputStream) throws Exception {
        if (inputStream == null) throw new CommonException("附件不能为空！");
        return saveFile(affixId, trueName, inputStream.readAllBytes());
    }

    public static String saveFile(String trueName, byte[] bytes) throws Exception {
        return saveFile(null, trueName, bytes);
    }

    public static String saveFile(String affixId, String trueName, byte[] bytes) throws Exception {
        if (bytes == null || bytes.length == 0) throw new CommonException("附件不能为空！");

        FileVO fileVO = FileServiceUtil.saveFile(trueName, bytes);

        AffixItemBO affixItem = new AffixItemBO();
        affixItem.setAffixId(affixId);
        affixItem.setSaveMode(fileVO.getMode());
        affixItem.setTrueName(fileVO.getName());
        affixItem.setSavePath(fileVO.getPath());
        affixItem.setFileSize(fileVO.getSize());
        affixItem.setCreateId(Constants.getCurrentUserId(false));
        affixItem.setCreateTime(System.currentTimeMillis());
        PaasContext.getBean(AffixItemService.class).save(affixItem);

        return affixItem.getId();
    }

    public static String saveFile(String filePath) throws Exception {
        if (Tool.isBlank(filePath)) return null;
        File file = new File(filePath);
        return file.exists() ? saveFile(file) : null;
    }

    public static String saveFile(File file) throws Exception {
        return saveFile(null, file.getName(), file);
    }

    public static String saveFile(String affixId, File file) throws Exception {
        return saveFile(affixId, file.getName(), file);
    }

    public static String saveFile(String affixId, String trueName, File file) throws Exception {
        if (file == null) throw new CommonException("附件不能为空！");

        FileVO fileVO = FileServiceUtil.saveFile(file);

        AffixItemBO affixItem = new AffixItemBO();
        affixItem.setAffixId(affixId);
        affixItem.setSaveMode(fileVO.getMode());
        affixItem.setTrueName(Tool.isBlank(trueName) ? fileVO.getName() : trueName);
        affixItem.setSavePath(fileVO.getPath());
        affixItem.setFileSize(fileVO.getSize());
        affixItem.setCreateId(Constants.getCurrentUserId(false));
        affixItem.setCreateTime(System.currentTimeMillis());
        PaasContext.getBean(AffixItemService.class).save(affixItem);

        return affixItem.getId();
    }

    public static String copyFile(String fileId) throws Exception {
        if (Tool.isBlank(fileId)) throw new CommonException("文件id不能为空！");

        AffixItemBO source = PaasContext.getBean(AffixItemService.class).queryBy(fileId);
        if (source == null) throw new CommonException("文件不存在！");

        byte[] bytes = FileServiceUtil.readFile(source.getSavePath());
        FileVO fileVO = FileServiceUtil.saveFile(source.getTrueName(), bytes);

        AffixItemBO target = new AffixItemBO();
        target.setTrueName(source.getTrueName());
        target.setSavePath(fileVO.getPath());
        target.setFileSize(source.getFileSize());
        target.setCreateId(Constants.getCurrentUserId(false));
        target.setCreateTime(System.currentTimeMillis());
        PaasContext.getBean(AffixItemService.class).save(target);

        return target.getId();
    }

    public static byte[] readFile(String fileId) throws Exception {
        AffixItemBO affixItem = PaasContext.getBean(AffixItemService.class).queryBy(fileId);
        return FileServiceUtil.readFile(affixItem.getSavePath());
    }

    public static String readFileToBase64(String fileId) throws Exception {
        AffixItemBO affixItem = PaasContext.getBean(AffixItemService.class).queryBy(fileId);
        return FileServiceUtil.readFileToBase64(affixItem.getSavePath());
    }

    public static byte[] readFile(File file) throws Exception {
        if (!file.exists()) throw new CommonException("文件不存在！");
        return Files.readAllBytes(file.toPath());
    }

    public static String readFileToBase64(File file) throws Exception {
        if (!file.exists()) throw new CommonException("文件不存在！");
        return Base64.getEncoder().encodeToString(Files.readAllBytes(file.toPath()));
    }
}
